'use strict';

angular.module('app')
	.controller('adSlotController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '广告位';
        $scope.param = { };
        $scope.loading = false;
        
		$scope.search = function () {
            $scope.param.approveMinTime=$("#approveMinTime").val();
            $scope.param.approveMaxTime=$("#approveMaxTime").val();
            $scope.param.createMinTime=$("#createMinTime").val();
            $scope.param.createMaxTime=$("#createMaxTime").val();
	        $scope.loading = true;
			$.ajax({
				url : $scope.app.project_name+'/adSlot/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.searchRe = function () {
			$scope.param.approveMinTime=$("#approveMinTime").val();
			$scope.param.approveMaxTime=$("#approveMaxTime").val();
			$scope.param.createMinTime=$("#createMinTime").val();
			$scope.param.createMaxTime=$("#createMaxTime").val();
	        $scope.loading = true;
	        $scope.param.pageNum = 1;
	        $.ajax({
				url : $scope.app.project_name+'/adSlot/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}

		$scope.selected = [];
		var updateSelected = function (action, contact) {
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id);
				contact.checked = true;
			}

			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
				contact.checked = false;
			}
		};

		$scope.updateSelection = function ($event, contact) {
			var checkbox = $event.target;
			var action = (checkbox.checked ? 'add' : 'remove');
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id);
				contact.checked = true;
			}
			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
				contact.checked = false;
			}
		};
		// 全选操作
		$scope.selectAll = function ($event) {
			var checkbox = $event.target;
			var action = (checkbox.checked ? 'add' : 'remove');
			for (var i = 0; i < $scope.pageInfo.records.length; i++) {
				var contact = $scope.pageInfo.records[i];
				updateSelected(action, contact);
			}
		};

		$scope.isSelected = function (id) {
			return $scope.selected.indexOf(id) >= 0;
		};

	//批量通过/失败
	$scope.batchApprove=function (state) {
        var remark=$('#remark').val();
        if(state==3) {
            if($scope.selected.length<=0){
                $scope.alertModal({"content":"请选择要审核的项"});
                return;
            }
            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;
            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.state=='2'&&item.checked){
                    count++;
                }else if(item.checked&&item.status!='2') {
                    nocount++;
                }
            })
            if(nocount>0){
                $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                return;
            }
            $scope.alertMessage({
                text: "确定做此操作吗？", //内容
                btnCancel: true, //取消按钮
                btnConfirm: true //确认按钮
            }).then(function () {
                $.ajax({
                    url: $scope.app.project_name + '/adSlot/update/batchApprove',
                    data: {'ids': $scope.selected, 'state': state,remark:'审核通过'}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.selected = [];
                    	$scope.alertModal({"content":'操作成功'});
                        $scope.param.keyword = null;

                    } else {
                        $scope.msg = result.msg;
                        $scope.alertModal({"content":'操作失败'});

                    }
                    $scope.search();
                    $scope.$apply()

                });
            });
        }else{
            $.ajax({
                url: $scope.app.project_name + '/adSlot/update/batchApprove',
                data: {'ids': $scope.selected, 'state': state,'remark':remark}
            }).then(function (result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.selected = [];
                    $scope.alertModal({"content":'操作成功'});
                    $scope.param.keyword = null;
                } else {
                    $scope.msg = result.msg;
                    $scope.alertModal({"content":'操作失败'});
                }
                $scope.search();
                $scope.firstApply = false;
                $('#remark').val("");
                $scope.$apply()

            });
		}

    }
    //点击改价按钮
     $scope.changePrice=function (id,state) {
		 document.getElementById("clickPrice"+id).style.display="none";
         document.getElementById("btn"+id).style.display="inline";
     }


     //修改价格
     $scope.updatePrice=function(operFlag,id,clickPrice){
         var price=$("#price"+id).val();
			if(price>=clickPrice){
				$scope.alertModal({"content":"服务价格不得高于广告位价格"});
				return;
			}
			if(price==0){
                $scope.alertModal({"content":"服务价必须大于0"});
                return;
            }
         $.ajax({
             url : $scope.app.project_name+'/adSlot/update/updateAdsoltPrice',
             data: {'id': id,adsoltPrice:price}
         }).then(function(result) {
             $scope.loading = false;
             if (result.httpCode == 200) {
                 $scope.alertModal({"content": "操作成功"});
                 window.location.reload()
             } else {
                 $scope.alertModal({"content": "操作失败"});
             }
             $scope.$apply();
         });
	 }

		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
        };

		$scope.preview = function(){
            if($scope.selected.length<=0){
                $scope.alertModal({"content":"请选择要审核的项"});
                return;
            }
            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;
            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.state=='2'&&item.checked){
                    count++;
                }else if(item.checked&&item.status!='2') {
                    nocount++;
                }
            })
            if(nocount>0){
                $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                return;
            }
			$scope.firstApply = true;
		}
} ]);